package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.core.domain.entity.SysDictData;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.system.mapper.SysDictDataMapper;
import com.hospital.workstation.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisVitalSignsBo;
import com.hospital.workstation.domain.vo.HisVitalSignsVo;
import com.hospital.workstation.domain.HisVitalSigns;
import com.hospital.workstation.mapper.HisVitalSignsMapper;
import com.hospital.workstation.service.IHisVitalSignsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 生命体征Service业务层处理
 *
 * @author whb
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class HisVitalSignsServiceImpl implements IHisVitalSignsService {

    private final HisVitalSignsMapper baseMapper;
    private final SysDictDataMapper dictDataMapper;

    /**
     * 查询生命体征
     */
    @Override
    public HisVitalSignsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询生命体征列表
     */
    @Override
    public TableDataInfo<HisVitalSignsVo> queryPageList(HisVitalSignsBo bo, PageQuery pageQuery) {
        // 查询生命体征信息
        LambdaQueryWrapper<HisVitalSigns> lqw = buildQueryWrapper(bo);
        Page<HisVitalSignsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 查询生命体征字典数据
        List<SysDictData> signTypeDictList = dictDataMapper.selectDictDataByType(DictConstants.SIGN_TYPE_DICT_TYPE);
        Map<String, String> signTypeMap = signTypeDictList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 设置生命体征名称
        result.getRecords().forEach(hisVitalSignsVo -> {
            hisVitalSignsVo.setSignTypeStr(signTypeMap.get(hisVitalSignsVo.getSignType()));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询生命体征列表
     */
    @Override
    public List<HisVitalSignsVo> queryList(HisVitalSignsBo bo) {
        LambdaQueryWrapper<HisVitalSigns> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisVitalSigns> buildQueryWrapper(HisVitalSignsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisVitalSigns> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisVitalSigns::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSignType()), HisVitalSigns::getSignType, bo.getSignType());
        lqw.eq(StringUtils.isNotBlank(bo.getSignResult()), HisVitalSigns::getSignResult, bo.getSignResult());
        lqw.eq(StringUtils.isNotBlank(bo.getMeasureUserCode()), HisVitalSigns::getMeasureUserCode, bo.getMeasureUserCode());
        lqw.eq(bo.getMeasureTime() != null, HisVitalSigns::getMeasureTime, bo.getMeasureTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReferenceValue()), HisVitalSigns::getReferenceValue, bo.getReferenceValue());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, HisVitalSigns::getMeasureTime,
            params.get("beginTime") + " 00:00:00", params.get("endTime") + " 23:59:59");
        lqw.orderByDesc(HisVitalSigns::getMeasureTime);
        return lqw;
    }

    /**
     * 新增生命体征
     */
    @Override
    public Boolean insertByBo(HisVitalSignsBo bo) {
        HisVitalSigns add = BeanUtil.toBean(bo, HisVitalSigns.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改生命体征
     */
    @Override
    public Boolean updateByBo(HisVitalSignsBo bo) {
        HisVitalSigns update = BeanUtil.toBean(bo, HisVitalSigns.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisVitalSigns entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生命体征
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
